<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Controllers_Target extends Gio_Core_Controller
{
	/**
	 * Apply hook for target
	 * 
	 * @return void
	 */
	public function addAction() 
	{
		$this->setNoRender();
		$this->disableLayout();
		
		$request 			   = $this->getRequest();
		$module  			   = $request->getPost('mod', '');
		if ('_' == $module) {
			$module = '';
		}
		$json 				   = new Services_JSON();
		$target   			   = $request->getPost('target');
		$target   			   = (array)$json->decode($target);
		$target['hook_name']   = $request->getPost('hook');
		$target['hook_module'] = $module;

		$id 				   = Modules_Core_Services_Target::add($target);
		
		if ($id > 0) {
			$this->getResponse()->setBody($id);
		} else {
			$this->getResponse()->setBody('RESULT_ERROR');
		}
	}
	
	public function listAction()
	{
		$modules = Gio_Core_File::getSubDir(ROOT_DIR . DS . 'modules');
		$targets = array();
		foreach ($modules as $module) {
			$info = Gio_Core_Hook_Config::getTargetInfo($module);
			if ($info) {
				$targets[$module] = $info;
			}
		}
		
		/**
		 * Get the list of hook modules
		 */
		$hookModules = array();
		$hooks 		 = array();
		$rs 		 = Modules_Core_Services_Hook::getModules();
		if ($rs) {
			foreach ($rs as $row) {
				$module 		= (null == $row['module'] || '' == $row['module']) 
								  ? '_' 
								  : $row['module'];
				$hooks[$module] = array();
				$hookModules[]  = $module;
			}
		}
		
		/**
		 * Get the list of hooks
		 */
		$rs = Modules_Core_Services_Hook::dbHooks(null, true);
		if ($rs) {
			foreach ($rs as $row) {
				if (null == $row['module'] || '' == $row['module']) {
					$row['module'] = '_';
				}
				$hooks[$row['module']][$row['name']] = $row;
			}
		}
					
		/** 
		 * ... and tagets
		 */
		$dbTargets = array();
		$rs 	   = Modules_Core_Services_Target::getTargets();
		if ($rs) {
			foreach ($rs as $row) {
				if (!isset($dbTargets[$row['target_name']])) {
					$dbTargets[$row['target_name']] = array();
				}
				$module = (null == $row['hook_module']) ? '_' : $row['hook_module'];
				$dbTargets[$row['target_name']][$row['target_id'] . ''] = $module . ':' . $row['hook_name'];
			}
		}
		
		$this->view->targets 	 = $targets;
		$this->view->hookModules = $hookModules;
		$this->view->hooks		 = $hooks;
		$this->view->dbTargets   = $dbTargets;
	}
	
	/**
	 * Remove hook from target
	 * 
	 * @return void
	 */
	public function removeAction() 
	{
		$this->setNoRender();
		$this->disableLayout();
		
		$request = $this->getRequest();
		$id 	 = $request->getPost('id');
		Modules_Core_Services_Target::delete($id);
		
		$this->getResponse()->setBody('RESULT_OK');
	}
}